package com.mavenq.fly.utils.excelv3;

import java.util.*;

/**
 * Excel 导入配置类（支持业务校验和行列错误定位）
 */
public class ExcelImportConfig<T> {
    // 必需配置
    private Class<T> targetClass;
    
    // 列映射配置
    private Map<String, String> columnMapping = new LinkedHashMap<>();
    private Map<Integer, String> indexMapping = new HashMap<>();
    
    // 验证配置
    private String[] expectedHeaders;
    private boolean strictHeaderCheck = false;
    private boolean allowExtraColumns = true;
    
    // 数据处理配置
    private boolean trimCellValue = true;
    private String dateFormat = "yyyy-MM-dd HH:mm:ss";
    private Map<String, CellValueConverter> customConverters = new HashMap<>();
    
    // 校验配置
    private Map<String, FieldValidator> fieldValidators = new HashMap<>();
    private List<BusinessValidator<T>> businessValidators = new ArrayList<>();
    private Set<String> requiredFields = new HashSet<>();
    
    // 回调接口
    private ImportListener<T> importListener;

    // 构造方法
    public ExcelImportConfig(Class<T> targetClass) {
        this.targetClass = targetClass;
    }

    // 建造者方法
    public ExcelImportConfig<T> columnMapping(String excelHeader, String fieldName) {
        this.columnMapping.put(excelHeader, fieldName);
        return this;
    }
    
    public ExcelImportConfig<T> indexMapping(int columnIndex, String fieldName) {
        this.indexMapping.put(columnIndex, fieldName);
        return this;
    }
    
    public ExcelImportConfig<T> expectedHeaders(String... headers) {
        this.expectedHeaders = headers;
        return this;
    }
    
    public ExcelImportConfig<T> strictHeaderCheck(boolean strict) {
        this.strictHeaderCheck = strict;
        return this;
    }
    
    public ExcelImportConfig<T> allowExtraColumns(boolean allow) {
        this.allowExtraColumns = allow;
        return this;
    }
    
    public ExcelImportConfig<T> trimCellValue(boolean trim) {
        this.trimCellValue = trim;
        return this;
    }
    
    public ExcelImportConfig<T> dateFormat(String format) {
        this.dateFormat = format;
        return this;
    }
    
    public ExcelImportConfig<T> customConverter(String fieldName, CellValueConverter converter) {
        this.customConverters.put(fieldName, converter);
        return this;
    }
    
    public ExcelImportConfig<T> importListener(ImportListener<T> listener) {
        this.importListener = listener;
        return this;
    }
    
    // 校验相关配置方法
    public ExcelImportConfig<T> addFieldValidator(String fieldName, FieldValidator validator) {
        this.fieldValidators.put(fieldName, validator);
        return this;
    }
    
    public ExcelImportConfig<T> addBusinessValidator(BusinessValidator<T> validator) {
        this.businessValidators.add(validator);
        return this;
    }
    
    public ExcelImportConfig<T> requiredFields(String... fields) {
        Collections.addAll(this.requiredFields, fields);
        return this;
    }
    
    // Getter 方法
    public Class<T> getTargetClass() { return targetClass; }
    public Map<String, String> getColumnMapping() { return columnMapping; }
    public Map<Integer, String> getIndexMapping() { return indexMapping; }
    public String[] getExpectedHeaders() { return expectedHeaders; }
    public boolean isStrictHeaderCheck() { return strictHeaderCheck; }
    public boolean isAllowExtraColumns() { return allowExtraColumns; }
    public boolean isTrimCellValue() { return trimCellValue; }
    public String getDateFormat() { return dateFormat; }
    public Map<String, CellValueConverter> getCustomConverters() { return customConverters; }
    public ImportListener<T> getImportListener() { return importListener; }
    public Map<String, FieldValidator> getFieldValidators() { return fieldValidators; }
    public List<BusinessValidator<T>> getBusinessValidators() { return businessValidators; }
    public Set<String> getRequiredFields() { return requiredFields; }
}